//index.js
const app = getApp()
const MAX_LITIM = 18;
let total = 0;

Page({
  data: {
    swiperImgs: [],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    playlist: [],
  },

  onLoad: function() {
    this._getSwiper();
    this._getPlaylist();
  },

  _getPlaylist() {
    wx.showLoading({
      title: '加载中',
    });
    wx.cloud.callFunction({
      name: "newMusic",
      data: {
        $url: "playlist",
        start: this.data.playlist.length,
        count: MAX_LITIM
      }
    }).then((res) => {
      wx.hideLoading();
      // console.log(res);
      const result = res.result;
      this.setData({
        playlist: this.data.playlist.concat(result.data)
      });
      total = result.total;
    });
  },

  _getSwiper() {
    wx.cloud.callFunction({
      name: "swiper"
    }).then((res) => {
      // console.log(res);
      const data = res.result.data;
      if (data.length > 0) {
        this.setData({
          swiperImgs: data
        });
      }
    });
  },

  // 跳转
  goMusic(e) {
    const id = e.target.dataset.id;
    wx.navigateTo({
      url: `/pages/musiclist/musiclist?id=${id}`,
    });
  },

  /**
   * 上拉加载
   */
  onReachBottom() {
    if (this.data.playlist.length < total) {
      this._getPlaylist();
    }
  },

  /**
   * 下拉刷新
   */
  onPullDownRefresh() {
    this.setData({
      swiperImgs: [],
      playlist: []
    });
    this._getSwiper();
    this._getPlaylist();
    wx.stopPullDownRefresh();
  }
})